#include "util.hpp"
#include "config.hpp"
#include "dataController.hpp"
#include "hotController.hpp"
#include "service.hpp"
#include <thread>


void testutil(const string& path){
    // cloud::FileUtil fu(path);
    // cout << fu.fileName() << endl;
    // cout << fu.fileSize() << endl;
    // cout << fu.lastModifyTime() << endl;
    // cout << fu.lastAddTime() << endl;

    // //读取文件数据放到body
    // string body;
    // fu.getContent(&body);
    // //将body中的内容写入test.txt
    // cloud::FileUtil nfu("./test.txt");
    // nfu.setContent(body);

    // string packname = path + ".lz";
    // //压缩
    // //用path实例化一个fu，将压缩后的数据放到packname中
    // cloud::FileUtil fu(path);
    // fu.compress(packname);
    // //解压
    // //由于压缩和解压都是对fu._path进行的，所以必须要实例化不同的FileUtil对象来管理要压缩或解压的文件
    // //用被压缩后的packname实例化一个pfu，将解压后的数据放到test.txt中，要注意不能解压到不存在的文件夹
    // cloud::FileUtil pfu(path);
    // pfu.uncompress("test.txt");

    cloud::FileUtil fu(path);
    fu.createDirectory();
    vector<string> arr;
    fu.scanDirectory(&arr);
    for(auto& a : arr){
        cout << a << endl;
    }
}

void jsonutiltest(){
    const char *name = "xiaoming";
    int age = 11;
    double score[] = {85, 67, 70.5};
    Json::Value root;
    root["姓名"] = name;
    root["年龄"] = age;
    root["成绩"].append(score[0]);
    root["成绩"].append(score[1]);
    root["成绩"].append(score[2]);
    string str;
    cloud::JsonUtil::serialize(root, &str);
    cout << str << endl;

    Json::Value val;
    cloud::JsonUtil::unserialize(str, &val);
    cout << val["姓名"].asString() << endl;
    cout << val["年龄"].asString() << endl;
    for (int i = 0; i < val["成绩"].size();i++){
        cout << val["成绩"][i].asDouble()<<" ";
    }
    cout << endl;
}

void testconfig(){
    //获取单例
    cloud::Config *config = cloud::Config::getInstance();
    cout << config->getHotTime() << endl;
    cout << config->getServerPort() << endl;
    cout << config->getServerIP() << endl;
    cout << config->getDownloadPrefix() << endl;
    cout << config->getPackfileSuffix() << endl;
    cout << config->getPackDir() << endl;
    cout << config->getBackupDir() << endl;
    cout << config->getBackupFile() << endl;
}

void testdatactlr(const string &filename){
    // cloud::BackupInfo info;
    // info.fillNewInfo(filename);
    // cout << "info------" << endl;
    // cout << info._iscompressed << endl;
    // cout << info._fsize << endl;
    // cout << info._lmtime << endl;
    // cout << info._latime << endl;
    // cout << info._fpath << endl;
    // cout << info._ppath << endl;
    // cout << info._url << endl;

    // cloud::DataController dctlr;
    // dctlr.insert(info);
    // cloud::BackupInfo tmp;
    // cout << endl;
    // dctlr.getByURL("/download/makefile", &tmp);
    // cout << "tmp insert and getByURL" << endl;
    // cout << tmp._iscompressed << endl;
    // cout << tmp._fsize << endl;
    // cout << tmp._lmtime << endl;
    // cout << tmp._latime << endl;
    // cout << tmp._fpath << endl;
    // cout << tmp._ppath << endl;
    // cout << tmp._url << endl;
    // info._iscompressed = true;

    // dctlr.modify(info);
    // vector<cloud::BackupInfo> arr;
    // dctlr.getAll(&arr);
    // cout << endl;
    // cout << "tmp modify and getAll" << endl;
    // for (auto a : arr){
    //     cout << a._iscompressed << endl;
    //     cout << a._fsize << endl;
    //     cout << a._lmtime << endl;
    //     cout << a._latime << endl;
    //     cout << a._fpath << endl;
    //     cout << a._ppath << endl;
    //     cout << a._url << endl;
    // }

    // dctlr.getByPath(filename, &tmp);
    // cout << endl;
    // cout << "tmp getByPath" << endl;
    // cout << tmp._iscompressed << endl;
    // cout << tmp._fsize << endl;
    // cout << tmp._lmtime << endl;
    // cout << tmp._latime << endl;
    // cout << tmp._fpath << endl;
    // cout << tmp._ppath << endl;
    // cout << tmp._url << endl;


    cloud::DataController dctlr;
    vector<cloud::BackupInfo> arr;
    dctlr.getAll(&arr);
    for (auto a : arr){
        cout << a._iscompressed << endl;
        cout << a._fsize << endl;
        cout << a._lmtime << endl;
        cout << a._latime << endl;
        cout << a._fpath << endl;
        cout << a._ppath << endl;
        cout << a._url << endl;
    }

}

cloud::DataController *_data;
void hottest(){
    cloud::HotController hot;
    hot.run();
}

void servicetest(){
    cloud::Service srv;
    srv.run();
}

int main(int argc, char* argv[]){

    _data = new cloud::DataController();
    // testutil(argv[1]);

    // jsonutiltest();

    // testconfig();

    // if(argc<2){
    //     cerr << "argc<2" << endl;
    //     return -1;
    // }
    // // testdatactlr(argv[1]);
    
    // hottest();

    // servicetest();

    thread thread_hot(hottest);
    thread thread_service(servicetest);
    
    thread_hot.join();
    thread_service.join();

    return 0;
}